﻿/*************************************
Function：主窗体
 Version：1.0
    Date：2012-04-28
  Author：rjdong
   Email：ooxxab@gmail.com
 WebSite：http://emake.info
*************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using Wind.Compressor.Business;
using Wind.Compressor.Entity;

namespace Wind.Compressor
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        #region private method


        private void bt_Open_Click()
        {
            try
            {
                if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.richTextBox2.Text = "";

                    this.toolStripStatusLabel1.Text = this.openFileDialog1.FileName;
                    using (FileStream fs = new FileStream(this.openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                        {
                            string strSource = sr.ReadToEnd();
                            /*
                            this.toolStripProgressBar1.Visible = true;
                            this.toolStripProgressBar1.Width = Form1.ActiveForm.Width;
                            while ((line = sr.ReadLine()) != null)
                            {
                                sb.Append(line);
                                this.toolStripProgressBar1.Value++;
                                if (this.toolStripProgressBar1.Value >= this.toolStripProgressBar1.Maximum)
                                    this.toolStripProgressBar1.Value = 0;
                            }
                            this.toolStripProgressBar1.Visible = false;
                           */
                            //byte[] unicodeBytes = sr.CurrentEncoding.GetBytes(sr.ReadToEnd());
                            //this.richTextBox1.Text = Encoding.UTF8.GetString(unicodeBytes);
                            this.richTextBox1.Text = strSource;
                            this.toolStripStatusLabel2.Text = "   Length:" + this.richTextBox1.Text.Length + "(byte)";
                            this.toolStripStatusLabel3.Text = "   Encode:" + sr.CurrentEncoding.EncodingName;
                            //this.toolStripStatusLabel3.Text = "   Encode:" + sr.CurrentEncoding.EncodingName + " Convert to:" + Encoding.UTF8.EncodingName;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.StackTrace);
                LogHelper.Write("Wind.Compressor.FormMain.bt_Open_Click error", ex);
            }
        }
        private void bt_Save_Click()
        {
            try
            {
                string filename = this.openFileDialog1.FileName;
                string name = Path.GetFileNameWithoutExtension(filename);
                string ext = Path.GetExtension(filename).ToLower(); 
                SettingInfo setting = Utils.LoadSetting();
                Encoding encoding = Utils.GetEncoding(setting.CssEncodeName);
                if (ext == ".css")
                {
                    if (setting.CssIsAutoSave)
                        name += setting.CssAutoSaveEndStr;
                    if (setting.CssIsVersion)
                        name += setting.CssVersion;
                }
                else if (ext == ".js")
                {
                    if (setting.JsIsAutoSave)
                        name += setting.JsAutoSaveEndStr;
                    if (setting.JsIsVersion)
                        name += setting.JsVersion;
                }
                this.saveFileDialog1.FileName = name + ext;
                if (!string.IsNullOrEmpty(this.richTextBox2.Text))
                {
                    if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        Utils.WriteFile(this.saveFileDialog1.FileName, this.richTextBox2.Text, encoding);
                        //this.richTextBox2.SaveFile(this.saveFileDialog1.FileName, RichTextBoxStreamType.UnicodePlainText);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.StackTrace);
                LogHelper.Write("Wind.Compressor.FormMain.bt_Save_Click error", ex);
            }
        }

        private void Compress()
        {
            try
            {
                if (!string.IsNullOrEmpty(this.richTextBox1.Text))
                {
                    SettingInfo setting = Utils.LoadSetting();
                    
                    this.richTextBox2.Text = YuiBusiness.Instance.DoCompress(this.openFileDialog1.FileName,
                        this.richTextBox1.Text,
                        setting,
                        this.openFileDialog1.FileName
                        );
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.StackTrace);
                LogHelper.Write("Wind.Compressor.FormMain.bt_Compress_Click error", ex);
            }
        }
        private void BatCompress()
        {
            try
            {
                FormBatCompress formBat = new FormBatCompress();
                formBat.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.StackTrace);
                LogHelper.Write("BatCompress error", ex);
            }
        }
       
        private void bt_Cut()
        {
            if (this.richTextBox1.Focused)
                this.richTextBox1.Cut();
            else if (this.richTextBox2.Focused)
                this.richTextBox2.Cut();
        }
        private void bt_Copy()
        {
            if (this.richTextBox1.Focused)
                this.richTextBox1.Copy();
            else if (this.richTextBox2.Focused)
                this.richTextBox2.Copy();
        }
        private void bt_Paste()
        {
            if (this.richTextBox1.Focused)
                this.richTextBox1.Paste();
            else if (this.richTextBox2.Focused)
                this.richTextBox2.Paste();
        }
        private void FormSetting_Show(int type)
        {
            FormSetting form2 = new FormSetting();
            form2.tabtype = type;
            form2.ShowDialog();
        }
        #endregion

        #region Menu & ToolButton

        private void 帮助LToolStripButton_Click(object sender, EventArgs e)
        {
            Tools.OpenHelp();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tools.OpenHelp();
        }

        private void 打开OToolStripButton_Click(object sender, EventArgs e)
        {
            bt_Open_Click();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bt_Open_Click();
        }
        private void 保存SToolStripButton_Click(object sender, EventArgs e)
        {
            bt_Save_Click();
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bt_Save_Click();
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Compress();
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认退出吗？未保存的文件将丢失！", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("确认退出吗？未保存的文件将丢失！", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void 剪切UToolStripButton_Click(object sender, EventArgs e)
        {
            bt_Cut();
        }

        private void 复制CToolStripButton_Click(object sender, EventArgs e)
        {
            bt_Copy();
        }

        private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
        {
            bt_Paste();
        }

        private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bt_Cut();
        }

        private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bt_Copy();
        }

        private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bt_Paste();
        }

        private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.richTextBox1.Focused)
                this.richTextBox1.SelectAll();
            else if (this.richTextBox2.Focused)
                this.richTextBox2.SelectAll();
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            FormSetting_Show(1);
        }

        private void toolStripDropDownButton2_Click(object sender, EventArgs e)
        {
            FormSetting_Show(2);
        }

        private void 选项OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormSetting_Show(1);
        }

        private void 批量压缩ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BatCompress();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            BatCompress();
        }
        #endregion
    }
}
